import os

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

import wsgiref.handlers

class OpenIdLoginHandler(webapp.RequestHandler):
    def get(self):
        cont = self.request.get('continue')
        template_values = {
            'continue': cont
            }
   
        path = os.path.join(os.path.dirname(__file__), 'templates', 'openid-login.html')
        self.response.out.write(template.render(path, template_values))        
   
    def post(self):
        cont = self.request.get('continue')
        #logging.info('OpenIDLogin handler called, cont: %s' % cont)
        openid = self.request.get('openid_url')
        if openid:
            #logging.info('creating login url for openid: %s' % openid)
            login_url = users.create_login_url(cont, None, openid)
            self.response.out.write(login_url)
            #logging.info('redirecting to url: %s' % login_url)
            #self.redirect(login_url)
        else:
            self.error(400)        
           
def main():
    url_map = [("/_ah/login_required", OpenIdLoginHandler)]
    application = webapp.WSGIApplication(url_map, debug=False)
    wsgiref.handlers.CGIHandler().run(application)
